package org.solr;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

public class ReIndex {
	static long tagTime;
	final static Random rnd = new Random();
	private String zkHost;
	private String defaultCollection = "collection1";
	int zkClientTimeout = 60000;
	int zkConnectTimeout = 5000;
	private String Base_URL;
	int count = 0;
	SolrServer solrServer;

	public ReIndex(String Base_URL) {
		this.Base_URL = Base_URL;
	}

	public ReIndex(final String zkHost, final String defaultCollection) {
		this.zkHost = zkHost;
		this.defaultCollection = defaultCollection;
	}

	public ReIndex(final String zkHost, final String defaultCollection,
			final int zkClientTimeout, final int zkConnectTimeout) {
		this.zkHost = zkHost;
		this.defaultCollection = defaultCollection;
		this.zkClientTimeout = zkClientTimeout;
		this.zkConnectTimeout = zkConnectTimeout;
	}

	public SolrServer getSolrServer() {
		if (Base_URL != null) {
			if (solrServer == null) {
				solrServer = new HttpSolrServer(Base_URL);
			}
		}

		if (zkHost != null && defaultCollection != null) {
			if (solrServer == null) {
				CloudSolrServer cloudSolrServer;
				try {
					cloudSolrServer = new CloudSolrServer(zkHost);
					cloudSolrServer.setDefaultCollection(defaultCollection);
					cloudSolrServer.setZkClientTimeout(zkClientTimeout);
					cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
					solrServer = cloudSolrServer;
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}

			}
		}

		return solrServer;
	}

	public void search(String q, int start, int rows) {
		long NumFound = 0;
		SolrDocumentList docs = null;
		Collection<SolrInputDocument> newdocs = null;
		SolrServer solrServer = getSolrServer();
		SolrQuery query = new SolrQuery();
		query.setQuery(q);
		query.setRows(rows);
		query.setStart(start);
		QueryResponse rsp;
		try {
			rsp = solrServer.query(query);
			docs = rsp.getResults();
			newdocs = new ArrayList<SolrInputDocument>();
			NumFound = docs.getNumFound();

			System.out.println("文档个数：" + NumFound);
			System.out.println("查询时间：" + rsp.getQTime());

		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (NumFound == 0) {
			return;
		}

		for (SolrDocument doc : docs) {
			count++;
			Collection<java.lang.String> fieldNames = doc.getFieldNames();
			SolrInputDocument solrInputDocument = new SolrInputDocument();
			
			for (String s : fieldNames) {
				if (!s.equals("_version_") && !s.equals("NewKeyword")) {
					if (s.equals("Content"))
						solrInputDocument.setField("NewKeyword", doc.getFieldValue(s));
					solrInputDocument.setField(s, doc.getFieldValue(s));
				}
			}
			newdocs.add(solrInputDocument);
		}
		try {
			solrServer.add(newdocs);
			solrServer.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("共更新索引: " + count);
		if ((start + rows) < NumFound) {
			search(q, start + rows, rows);
		}

	}

	public static void main(String[] args) throws SolrServerException,
			IOException {
		 String solr_url = "http://localhost:8983/solr/collection1";
		String zkHost = "localhost:2181";
		String collection = "collection1";
		 ReIndex reIndex = new ReIndex(solr_url);
//		ReIndex reIndex = new ReIndex(zkHost, collection);
		reIndex.search("*:*", 0, 10000);

	}

}
